草庐IT

C++ typedef 结构与类

全部标签

go - 将通用结构/接口(interface)传递给函数并返回它

我可以将通用结构或接口(interface)传递给函数,然后返回它吗?我试过在下面的例子中使用指针,我也试过使用struct作为返回类型,但我似乎做不到。如果我改为使用interface{},我似乎能够传入postData,但通过返回或更新指针来取回它似乎是不可能的。谁能告诉我哪里出错了?funcEmailHandler(writerhttp.ResponseWriter,request*http.Request){varpostData=EmailPostData{}ConvertRequestJsonToJson(request,&postData)}funcConvertRequ

go - 在函数中返回结构的值并在另一个包中使用它

我尝试在我的示例项目中创建包配置,但有些东西没有像我预期的那样工作,我有文件夹结构:config/config.go//packageconfigmain.go//packagemain我想在我的主文件中使用配置:funcmain(){conf:=config.GetConf()db:=dbConn{schemas:map[string]*sql.DB{},url:fmt.Sprintf("tcp(%s)",conf.db['dev']),username:db.user,password:db.password,}db.create()}我的配置文件:typeConfigstruct

go - 通过引用传递接收者结构

我需要一个函数来返回一个由调用者传递给函数的结构。每次调用函数时,结构都会不同。我正在使用ORMpostgres驱动程序“github.com/go-pg/pg”。据我了解,ORM要求在调用.Query()之前定义结构,后者使用数据库中的数据填充结构。funcPgSql(userUser,statementstring)(output[]string,errerror){_,err=db.Query(&users,`SELECT*FROMstandard_lookupWHEREpkid_='STATE|AFKDZ'`)}结果将在用户结构中。但是,我需要在不同的包中定义这个结构并将其传递

go - Golang 中的结构声明

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion鉴于Go主要基于C,并且该语言中的结构定义如下:structPerson{...}为什么我们在Go中有额外的词?typePersonstruct{...}为什么我们需要同时提到类型和结构?似乎有点冗长。

go - 解码映射结构

首先我有一个结构:typetimesmap[time.Time]struct{}我需要为它编写Marshal/Unmarshal方法来转换json。我写过MarshalJSON方法,但不明白如何写UnmarshalJSON方法。func(tstimes)keys()[]time.Time{res:=make([]time.Time,0,len(ts))forkey:=rangets{res=append(res,key)}returnres}func(tstimes)MarshalJSON()([]byte,error){returnjson.Marshal(ts.keys())}fu

json - 如何在 Go 中导航具有未知结构的嵌套 json?

我正在尝试从GoLang中的深层嵌套json数据中解析和获取选定数据。我在浏览结构和访问数据时遇到问题。数据太深太复杂,无法使用Go中的先验已知结构进行解析。这是文件的URL:-https://www.data.gouv.fr/api/1/datasets/?format=csv&page=0&page_size=20我用map接口(interface)做了一些解析并使用了一个json字符串:resultdata:=map[string]interface{}json.Unmarshal([]byte(inputbytestring),&resultdata)//Inputstring

go - 被嵌入在golang结构中的接口(interface)所困惑

像这里的代码,当把一个接口(interface)A嵌入到结构体B中,然后将A设置为aa,aa是AA的一个实例。B和AA都有元素X,当调用b.X时我刚得到B.X。我怎样才能得到b.AA.X?我知道这个语法是有线的,但我只是想弄清楚varb是如何存储在内存中的,我尝试了一些不安全的语法,没办法得到b.A.X.:packagemainimport("fmt""unsafe")typeAinterface{Hello()string}typeBstruct{AXstring}typeAAstruct{numintXstring}func(aaAA)Hello()string{returnfmt

go - 获取外部/父结构名称

我正面临一个Golang初学者问题,我不知道如何正确解决它。你能帮帮我吗?信息:尽管这违背了Go的概念(不是试图成为一种OOP语言),但我仍然想讨论一些解决方案。我想知道接收器/子结构中的外部/父结构名称。请查看以下代码(Playground:https://play.golang.org/p/h6dARJQwidS)packagemainimport("fmt""reflect")typeParentstruct{Iduint32}func(p*Parent)GetStructName()string{returnreflect.TypeOf(p).Elem().Name()}typ

作为结构成员的函数

我正在尝试使函数成为我的结构中的成员typemyStructstruct{myFunfunc(interface{})interface{}}functestFunc1(bbool)bool{//somefunctionalityhere//returnsabooleanattheend}functestFunc2(sstring)int{//somefunctionalitylikemeasuringthestringlength//returnsanintegerindicatingthelength}funcmain(){fr:=myStruct{testFunc1}gr:=my

arrays - Golang : Parsing two array of arrays in JSON, 使用相同的结构

这个问题在这里已经有了答案:Unmarshalingjsonintoatype(2个答案)HowtoUnmarshalthepairvaluesinannestedarraybyjsonforGolang(2个答案)UnmarshalJsondatainaspecificstruct(1个回答)关闭3年前。假设我有这个数据:{"code":10000,"msg":"Successfulrequestprocessing","asks":[["0.03434400","0.31100000"],["0.03436300","0.18900000"],[],"bids":[["0.0342